package model

import (
	"github.com/jinzhu/gorm"
	_const "go-admin/internal/model/const"
)

// 角色
type Role struct {
	CommonModel
	IsBanned string `json:"isBanned"` // 是否禁用
	Remark   string `json:"remark"`   // 备注信息
	RoleName string `json:"roleName"` // 角色名称
	RoleKey  string `json:"roleKey"`  // 角色key
}

// 重置表名
func (r *Role) TableName() string {
	return "sys_role"
}

// 获取总数
func (r *Role) Count(db *gorm.DB) (int, error) {
	var count int
	if r.IsBanned != "" {
		db = db.Where("is_banned = ?", r.IsBanned)
	}
	if r.RoleName != "" {
		db = db.Where("role_name LIKE ?", "%"+r.RoleName+"%")
	}
	if err := db.Model(&r).Where("is_del = ?", _const.ISDEL).Count(&count).Error; err != nil {
		return 0, err
	}
	return count, nil
}

// 创建
func (r *Role) Create(db *gorm.DB) error {
	return db.Create(&r).Error
}

// 获取列表
func (r *Role) List(db *gorm.DB, pageOffset, pageSize int) ([]*Role, error) {
	var roles []*Role
	var err error
	db = db.Offset(pageOffset).Limit(pageSize)
	if r.IsBanned != "" {
		db = db.Where("is_banned = ?", r.IsBanned)
	}
	if r.RoleName != "" {
		db = db.Where("role_name LIKE ?", "%"+r.RoleName+"%")
	}
	if err = db.Where("is_del = ?", _const.ISDEL).Find(&roles).Error; err != nil {
		return nil, err
	}
	return roles, nil
}

// 更新
func (r *Role) Update(db *gorm.DB, values interface{}) error {
	if err := db.Model(&r).Updates(values).Where("id = ?", r.Id).Error; err != nil {
		return err
	}
	return nil
}

// 软删除
func (r *Role) Delete(db *gorm.DB, values interface{}) error {
	if err := db.Model(&r).Updates(values).Where("id = ?", r.Id).Error; err != nil {
		return err
	}
	return nil
}

// 批量软删除
func (r *Role) Deletes(db *gorm.DB, ids []string, values interface{}) error {
	return db.Model(&r).Where("id IN (?)", ids).Updates(values).Error
}

func (r *Role) GetRoleByJobNameAndJobCode(db *gorm.DB, roleName, roleKey string) (*Role, error) {
	var role Role
	if err := db.Where("is_del = ?", _const.ISDEL).Where("role_name = ?", roleName).Or("role_key = ?", roleKey).First(&role).Error; err != nil {
		// 判断错误
		switch {
		case err == gorm.ErrRecordNotFound:
			// 说明没有找到
			return &role, nil
		case err != nil:
			return &role, err
		}
	}
	return &role, nil
}

// 根据id获取详情
func (r *Role) GetRoleById(db *gorm.DB) (*Role, error) {
	var role Role
	if err := db.Where("id = ?", r.Id).First(&role).Error; err != nil {
		// 判断错误
		switch {
		case err == gorm.ErrRecordNotFound:
			// 说明没有找到
			return &role, nil
		case err != nil:
			return &role, err
		}
	}
	return &role, nil
}

// 获取列表
func (r *Role) RoleListAll(db *gorm.DB) ([]*Role, error) {
	var roles []*Role
	var err error
	if err = db.Where("is_del = ?", _const.ISDEL).Where("is_banned = ?", _const.ISBANNED).Find(&roles).Error; err != nil {
		return nil, err
	}
	return roles, nil
}

// 根据用户名获取用户角色集合
func (r *Role) GetRolesByUserName(db *gorm.DB, username string) ([]*Role, error) {
	roles := make([]*Role, 0)
	return roles, db.Table("sys_user su").Select("sr.*").Joins("LEFT JOIN sys_user_role sur ON su.id = sur.user_id").Joins("LEFT JOIN sys_role sr ON sr.id = sur.role_id").Where("su.username = ?", username).Find(&roles).Error
}

// 根据角色id集合获取角色
func (r *Role) GetRolesByRoleIds(db *gorm.DB, roleIds []string) ([]*Role, error) {
	roles := make([]*Role, 0)
	if len(roleIds) == 0 {
		return roles, nil
	}
	if err := db.Where("id IN (?)", roleIds).Where("is_del = ?", _const.ISDEL).Where("is_banned = ?", _const.ISBANNED).Find(&roles).Error; err != nil {
		return roles, err
	}
	return roles, nil
}
